*********************************************************************************
* 																				*
*	Campaign Panel Book Do-File Chapter 2 -	Intraindividual Dynamic of Events	*		
*																				*
*********************************************************************************

*Analysis of dynamics is done in the same way as shown in do-file "dynamic reception" and is therefore not explained again.


*---------------------------------------------------------------
* Creating the main diagonal for successive wave pairs and combined for all waves
*---------------------------------------------------------------



foreach num1 of numlist 2/7  {
foreach num2 of numlist 2/7  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist b_nsa_handling`num1'  {		
		gen b_nsa_handling`num1'`num2'_hd=.
		replace b_nsa_handling`num1'`num2'_hd=1 if b_nsa_handling`num1'==b_nsa_handling`num2' & b_nsa_handling`num1'<. & b_nsa_handling`num2'<.
		replace b_nsa_handling`num1'`num2'_hd=0 if b_nsa_handling`num1'!=b_nsa_handling`num2' & b_nsa_handling`num1'<. & b_nsa_handling`num2'<.
		lab var b_nsa_handling`num1'`num2'_hd "Hauptdiagonale: NSA handling"
		tab b_nsa_handling`num1'`num2'_hd [aweight=b_weight], m				
				}
		}
	}
	}

foreach num1 of numlist 1 3 5 7  {
foreach num2 of numlist 1 3 5 7  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist a_allwilag`num1'  {		
		gen a_allwilag`num1'`num2'_hd=.
		replace a_allwilag`num1'`num2'_hd=1 if a_allwilag`num1'==a_allwilag`num2' & a_allwilag`num1'<. & a_allwilag`num2'<.
		replace a_allwilag`num1'`num2'_hd=0 if a_allwilag`num1'!=a_allwilag`num2' & a_allwilag`num1'<. & a_allwilag`num2'<.
		lab var a_allwilag`num1'`num2'_hd "Hauptdiagonale: Economy 2009"
		tab a_allwilag`num1'`num2'_hd [aweight=a_weight], m				
				}
		}
	}
	}

foreach num1 of numlist 1 4 7  {
foreach num2 of numlist 1 4 7  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist b_allwilag`num1'  {		
		gen b_allwilag`num1'`num2'_hd=.
		replace b_allwilag`num1'`num2'_hd=1 if b_allwilag`num1'==b_allwilag`num2' & b_allwilag`num1'<. & b_allwilag`num2'<.
		replace b_allwilag`num1'`num2'_hd=0 if b_allwilag`num1'!=b_allwilag`num2' & b_allwilag`num1'<. & b_allwilag`num2'<.
		lab var b_allwilag`num1'`num2'_hd "Hauptdiagonale: Economy 2013"
		tab b_allwilag`num1'`num2'_hd [aweight=b_weight], m				
				}
		}
	}
	}

foreach num1 of numlist 5 6 7  {
foreach num2 of numlist 5 6 7  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist b_syria_handling`num1'  {		
		gen b_syria_handling`num1'`num2'_hd=.
		replace b_syria_handling`num1'`num2'_hd=1 if b_syria_handling`num1'==b_syria_handling`num2' & b_syria_handling`num1'<. & b_syria_handling`num2'<.
		replace b_syria_handling`num1'`num2'_hd=0 if b_syria_handling`num1'!=b_syria_handling`num2' & b_syria_handling`num1'<. & b_syria_handling`num2'<.
		lab var b_syria_handling`num1'`num2'_hd "Hauptdiagonale: Syria 2013"
		tab b_syria_handling`num1'`num2'_hd [aweight=b_weight], m				
				}
		}
	}
	}

	
foreach num1 of numlist 4 6 7  {
foreach num2 of numlist 4 6 7  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist b_veggie`num1'  {		
		gen b_veggie`num1'`num2'_hd=.
		replace b_veggie`num1'`num2'_hd=1 if b_veggie`num1'==b_veggie`num2' & b_veggie`num1'<. & b_veggie`num2'<.
		replace b_veggie`num1'`num2'_hd=0 if b_veggie`num1'!=b_veggie`num2' & b_veggie`num1'<. & b_veggie`num2'<.
		lab var b_veggie`num1'`num2'_hd "Hauptdiagonale: Veggie 2013"
		tab b_veggie`num1'`num2'_hd [aweight=b_weight], m				
				}
		foreach var1 of varlist b_maut`num1'  {		
		gen b_maut`num1'`num2'_hd=.
		replace b_maut`num1'`num2'_hd=1 if b_maut`num1'==b_maut`num2' & b_maut`num1'<. & b_maut`num2'<.
		replace b_maut`num1'`num2'_hd=0 if b_maut`num1'!=b_maut`num2' & b_maut`num1'<. & b_maut`num2'<.
		lab var b_maut`num1'`num2'_hd "Hauptdiagonale: Maut 2013"
		tab b_maut`num1'`num2'_hd [aweight=b_weight], m				
				}
		}
	}
	}

foreach num1 of numlist 3 5 6  {
foreach num2 of numlist 3 5 6  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist b_glaubw_rrg`num1'  {		
		gen b_glaubw_rrg`num1'`num2'_hd=.
		replace b_glaubw_rrg`num1'`num2'_hd=1 if b_glaubw_rrg`num1'==b_glaubw_rrg`num2' & b_glaubw_rrg`num1'<. & b_glaubw_rrg`num2'<.
		replace b_glaubw_rrg`num1'`num2'_hd=0 if b_glaubw_rrg`num1'!=b_glaubw_rrg`num2' & b_glaubw_rrg`num1'<. & b_glaubw_rrg`num2'<.
		lab var b_glaubw_rrg`num1'`num2'_hd "Hauptdiagonale: Credibility RRG 2013"
		tab b_glaubw_rrg`num1'`num2'_hd [aweight=b_weight], m				
				}
		}
	}
	}

		
		

	
****Aggregation on a complete main diagonal for all successive waves


gen a_allwilag_hd_ges = (a_allwilag13_hd+a_allwilag35_hd+a_allwilag57_hd)/3
gen b_allwilag_hd_ges = (b_allwilag14_hd+b_allwilag47_hd)/2

gen b_nsa_handling_hd_ges=(b_nsa_handling23_hd+b_nsa_handling34_hd+b_nsa_handling45_hd+b_nsa_handling56_hd+b_nsa_handling67_hd)/5

gen b_syria_handling_hd_ges = (b_syria_handling56_hd+b_syria_handling67_hd)/2
gen b_veggie_hd_ges = (b_veggie46_hd+b_veggie67_hd)/2
gen b_maut_hd_ges = (b_maut46_hd+b_maut67_hd)/2
gen b_glaubw_rrg_hd_ges = (b_glaubw_rrg35_hd+b_glaubw_rrg56_hd)/2


****Drop single main diagonals



foreach var in a_allwilag b_allwilag b_nsa_handling b_syria_handling b_veggie b_maut b_glaubw_rrg {
		drop `var'??_hd
	}


*We save the data before running the intra-individual regressions
save "${data}\data2009_2013 participants 7 waves.dta", replace







*---------------------------------------------------------------------
* Intraindividual Dynamic: Individual Regression over Time
*----------------------------------------------------------------------

* Creating ID-variable for loops

/*By employing a loop over all respondents, idividual regression lines should be
calculated for mode of reception over time. Since not all respondents possess valid
values for all reception variables in each wave (refuse to answer/do not know),
the loop would stop when encountering such a case. This is the reason for the
ID-variable which only contains values for those respondents where the conditions
are met. This guarantees the functioning of the loop without problems for this
new variable.*/



foreach year in a b {
foreach var in allwilag {
egen `year'_id_`var' = group(lfdn) if `year'_`var'_hd_ges!=.	//Decision: no missings allowed
}
}


foreach year in b {
foreach var in nsa_handling syria_handling veggie maut glaubw_rrg {
egen `year'_id_`var' = group(lfdn) if `year'_`var'_hd_ges!=.	//Decision: no missings allowed
}
}



* Transform data into long format

keep a_allwilag* b_allwilag* b_nsa_handling* b_syria_handling* b_veggie* b_maut* ///
	b_glaubw_rrg* lfdn a_id* b_id*
reshape long a_allwilag b_allwilag b_nsa_handling b_syria_handling b_veggie b_maut ///
	b_glaubw_rrg, i(lfdn) j(welle)

		
	
* Intraindividual linear regression

/*For each individual we want to know whether reception increases, decreases or 
remains stable during the campaign. This information is saved in `year'_koeff_`var'*/


foreach year in a b {
foreach var in allwilag  {

* Create outcome variable
gen `year'_koeff_`var' = .
gen `year'_rohkoeff_`var' = . 

* Loop over all respondents to calculate regression:
* Fill target variable with values

sum `year'_id_`var'
forvalues num=1/`r(max)' {	
quietly reg `year'_`var' welle ///
	if `year'_id_`var'==`num'
replace `year'_rohkoeff_`var' = _b[welle] ///
	if `year'_id_`var'==`num'
gen p = 2*ttail(e(df_r), abs(_b[welle]/_se[welle])) ///
	if `year'_id_`var'==`num' 		//calcualte p-value and save in variable
replace `year'_koeff_`var' = -1 ///
	if _b[welle]<0 & p<0.05 & `year'_id_`var'==`num' 
replace `year'_koeff_`var' = 0 ///
	if p>0.05 & `year'_id_`var'==`num' 
replace `year'_koeff_`var' = 1 ///
	if _b[welle]>0 & p<0.05 & `year'_id_`var'==`num' 
drop p

}
}
}

foreach year in b {
foreach var in nsa_handling syria_handling veggie maut glaubw_rrg  {

* Create outcome variable
gen `year'_koeff_`var' = .
gen `year'_rohkoeff_`var' = . 

* Loop over all respondents to calculate regression:
* Fill target variable with values


sum `year'_id_`var'
forvalues num=1/`r(max)' {	
quietly reg `year'_`var' welle ///
	if `year'_id_`var'==`num'
replace `year'_rohkoeff_`var' = _b[welle] ///
	if `year'_id_`var'==`num'
gen p = 2*ttail(e(df_r), abs(_b[welle]/_se[welle])) ///
	if `year'_id_`var'==`num' 		//calcualte p-value and save in variable
replace `year'_koeff_`var' = -1 ///
	if _b[welle]<0 & p<0.05 & `year'_id_`var'==`num' 
replace `year'_koeff_`var' = 0 ///
	if p>0.05 & `year'_id_`var'==`num' 
replace `year'_koeff_`var' = 1 ///
	if _b[welle]>0 & p<0.05 & `year'_id_`var'==`num' 
drop p

}
}
}



fre *koeff*	

										
//check  number of cases
sum b_id_veggie
sum b_koeff_veggie	//correct!


* Save coefficients in new dataset since procedure is very time-consuming
reshape wide a_allwilag b_allwilag b_nsa_handling b_syria_handling b_veggie b_maut ///
	b_glaubw_rrg a_koeff_allwilag b_koeff_allwilag b_koeff_nsa_handling b_koeff_syria_handling b_koeff_veggie b_koeff_maut ///
	b_koeff_glaubw_rrg a_rohkoeff_allwilag b_rohkoeff_allwilag b_rohkoeff_nsa_handling b_rohkoeff_syria_handling b_rohkoeff_veggie b_rohkoeff_maut ///
	b_rohkoeff_glaubw_rrg,	i(lfdn) j(welle)
	
keep lfdn ?_koeff_*7 ?_rohkoeff_*7 	?_koeff_glaubw_rrg6 ?_rohkoeff_glaubw_rrg6	
							//Coefficient-variable from one wave is sufficient,
							//since it's constant (after all the coefficient was 
							//calculated using all waves)
							
foreach ding in allwilag nsa_handling syria_handling veggie maut {
rename ?_koeff_`ding'7 ?_koeff_`ding'		//Waveending gone, since constant
rename ?_rohkoeff_`ding'7 ?_rohkoeff_`ding'
}

foreach ding in glaubw_rrg {
rename ?_koeff_`ding'6 ?_koeff_`ding'		//Waveending gone, since constant
rename ?_rohkoeff_`ding'6 ?_rohkoeff_`ding'
}

lab define koeff ///
	-1 "negativer Koeffizient" ///
	0 "insignifikanter Koeffizient" ///
	1 "positiver Koeffizient"
lab val ?_koeff_* koeff

save "${data}\data 2009 2013 IIR Chapter 2 incidents.dta", replace


*-----------------------------------------------------------------
* Table: Stability / Dynamic of Receptionfrequencies
*----------------------------------------------------------------

/* Measure of stability: average main diagonal percentages crossed with
intraindividual regression coefficients.
First, merge coefficients (to data)*/

use "${data}\data2009_2013 participants 7 waves.dta",clear

//drop _merge
merge 1:1 lfdn using "${data}\data 2009 2013 IIR Chapter 2 incidents.dta"
drop _merge

lab define koeff ///
	-1 "negativer Koeffizient" ///
	0 "insignifikanter Koeffizient" ///
	1 "positiver Koeffizient", replace
lab val ?_koeff_* koeff

* Categorize raw coefficients (without test of significance)


foreach year in a b {
	foreach var in allwilag {
		gen `year'_rohkoeffkat_`var' = `year'_rohkoeff_`var'
		replace `year'_rohkoeffkat_`var' = -1 if `year'_rohkoeff_`var'<0
		replace `year'_rohkoeffkat_`var' = 1 if `year'_rohkoeff_`var'>0 & `year'_rohkoeff_`var'~=.
	}
}

foreach year in b {
	foreach var in nsa_handling syria_handling veggie maut glaubw_rrg {
		gen `year'_rohkoeffkat_`var' = `year'_rohkoeff_`var'
		replace `year'_rohkoeffkat_`var' = -1 if `year'_rohkoeff_`var'<0
		replace `year'_rohkoeffkat_`var' = 1 if `year'_rohkoeff_`var'>0 & `year'_rohkoeff_`var'~=.
	}
}


**************************************************
***Tabels 13 and 14
**************************************************		
		
***Aggretate wave means
estimates clear

sum a_allwilag1 if a_allwilag_hd_ges~=.		//static mean, w1
gen a_meanallwilag1 = r(mean)

sum a_allwilag3 if a_allwilag_hd_ges~=.		//static mean, w3
gen a_meanallwilag3 = r(mean)

sum a_allwilag5 if a_allwilag_hd_ges~=.		//static mean, w5
gen a_meanallwilag5 = r(mean)

sum a_allwilag7 if a_allwilag_hd_ges~=.		//static mean, w7
gen a_meanallwilag7 = r(mean)

gen a_gesmeanallwilag = (a_meanallwilag1 + a_meanallwilag3 + a_meanallwilag5 + a_meanallwilag7)/4
tab a_gesmeanallwilag [aweight=a_weight]

gen a_gessdallwilag = sqrt(((a_meanallwilag1-a_gesmeanallwilag)^2 + (a_meanallwilag3-a_gesmeanallwilag)^2 + (a_meanallwilag5-a_gesmeanallwilag)^2 + (a_meanallwilag7-a_gesmeanallwilag)^2)/4) 
tab a_gessdallwilag [aweight=a_weight]


foreach num of numlist 1 4 5 6 7 808{
foreach var in allwilag1 allwilag3 allwilag5 allwilag7 {
	egen a_meanp`var'`num' = mean(a_`var') if a_`var'!=. & a_pidfull1==`num'
}
*
gen a_gesmeanallwilagp`num' = (a_meanpallwilag1`num' + a_meanpallwilag3`num' + a_meanpallwilag5`num' + a_meanpallwilag7`num')/4
tab a_gesmeanallwilagp`num' [aweight=a_weight]

gen a_gessdallwilagp`num' = sqrt(((a_meanpallwilag1`num'-a_gesmeanallwilagp`num')^2 + (a_meanpallwilag3`num'-a_gesmeanallwilagp`num')^2 + (a_meanpallwilag5`num'-a_gesmeanallwilagp`num')^2 + (a_meanpallwilag7`num'-a_gesmeanallwilagp`num')^2)/4) 
tab a_gessdallwilagp`num' [aweight=a_weight]
}


*Aggregate stability preparation


		foreach num of numlist 1 3 5 7 {
	gen a_allwilag`num'n = round(a_allwilag`num'*4)
	tab a_allwilag`num'n
}
*

*Differences
gen a_diff_allwilag13=a_allwilag3n-a_allwilag1n
tab a_diff_allwilag13

gen a_diff_allwilag35=a_allwilag5n-a_allwilag3n
tab a_diff_allwilag35

gen a_diff_allwilag57=a_allwilag7n-a_allwilag5n
tab a_diff_allwilag57

gen a_stab_allwilag13 =1-abs(a_diff_allwilag13/4) if a_allwilag1n==0 | a_allwilag1n==4
replace a_stab_allwilag13 =1-abs(a_diff_allwilag13/3) if a_allwilag1n==1 | a_allwilag1n==3
replace a_stab_allwilag13 =1-abs(a_diff_allwilag13/2) if a_allwilag1n==2
tab a_stab_allwilag13

gen a_stab_allwilag35 =1-abs(a_diff_allwilag35/4) if a_allwilag3n==0 | a_allwilag3n==4
replace a_stab_allwilag35 =1-abs(a_diff_allwilag35/3) if a_allwilag3n==1 | a_allwilag3n==3
replace a_stab_allwilag35 =1-abs(a_diff_allwilag35/2) if a_allwilag3n==2
tab a_stab_allwilag35

gen a_stab_allwilag57 =1-abs(a_diff_allwilag57/4) if a_allwilag5n==0 | a_allwilag5n==4
replace a_stab_allwilag57 =1-abs(a_diff_allwilag57/3) if a_allwilag5n==1 | a_allwilag5n==3
replace a_stab_allwilag57 =1-abs(a_diff_allwilag57/2) if a_allwilag5n==2
tab a_stab_allwilag57


foreach num in 13 35 57 {
	ci means a_stab_allwilag`num'
	gen a_stab_allwilag`num'_mean = r(mean)
}

*

*Aggregate stability
foreach year in a{
	foreach var in allwilag {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'13_mean+`year'_stab_`var'35_mean+`year'_stab_`var'57_mean)/3
		tab `year'_stab_`var'_aggr
}
}






*Economy 2013
sum b_allwilag1 if b_allwilag_hd_ges~=.		//static mean, w1
gen b_meanallwilag1 = r(mean)

sum b_allwilag4 if b_allwilag_hd_ges~=.		//static mean, w4
gen b_meanallwilag4 = r(mean)

sum b_allwilag7 if b_allwilag_hd_ges~=.		//static mean, w7
gen b_meanallwilag7 = r(mean)

gen b_gesmeanallwilag = (b_meanallwilag1 + b_meanallwilag4 + b_meanallwilag7)/3
tab b_gesmeanallwilag [aweight=b_weight]

gen b_gessdallwilag = sqrt(((b_meanallwilag1-b_gesmeanallwilag)^2 + (b_meanallwilag4-b_gesmeanallwilag)^2 + (b_meanallwilag7-b_gesmeanallwilag)^2)/3) 
tab b_gessdallwilag [aweight=b_weight]


foreach num of numlist 1 4 5 6 7 808{
foreach var in allwilag1 allwilag4 allwilag7 {
	egen b_meanp`var'`num' = mean(b_`var') if b_`var'!=. & b_pidfull1==`num'
}
*
gen b_gesmeanallwilagp`num' = (b_meanpallwilag1`num' + b_meanpallwilag4`num' + b_meanpallwilag7`num')/3
tab b_gesmeanallwilagp`num' [aweight=b_weight]

gen b_gessdallwilagp`num' = sqrt(((b_meanpallwilag1`num'-b_gesmeanallwilagp`num')^2 + (b_meanpallwilag4`num'-b_gesmeanallwilagp`num')^2  + (b_meanpallwilag7`num'-b_gesmeanallwilagp`num')^2)/3) 
tab b_gessdallwilagp`num' [aweight=b_weight]
}


*Aggregate stability preparation


		foreach num of numlist 1 4 7 {
	gen b_allwilag`num'n = round(b_allwilag`num'*4)
	tab b_allwilag`num'n
}
*

*Differences
gen b_diff_allwilag14=b_allwilag4n-b_allwilag1n
tab b_diff_allwilag14

gen b_diff_allwilag47=b_allwilag7n-b_allwilag4n
tab b_diff_allwilag47

gen b_stab_allwilag14 =1-abs(b_diff_allwilag14/4) if b_allwilag1n==0 | b_allwilag1n==4
replace b_stab_allwilag14 =1-abs(b_diff_allwilag14/3) if b_allwilag1n==1 | b_allwilag1n==3
replace b_stab_allwilag14 =1-abs(b_diff_allwilag14/2) if b_allwilag1n==2
tab b_stab_allwilag14

gen b_stab_allwilag47 =1-abs(b_diff_allwilag47/4) if b_allwilag4n==0 | b_allwilag4n==4
replace b_stab_allwilag47 =1-abs(b_diff_allwilag47/3) if b_allwilag4n==1 | b_allwilag4n==3
replace b_stab_allwilag47 =1-abs(b_diff_allwilag47/2) if b_allwilag4n==2
tab b_stab_allwilag47

foreach num in 14 47 {
	ci means b_stab_allwilag`num'
	gen b_stab_allwilag`num'_mean = r(mean)
}

*

*Aggregate stability
foreach year in b {
	foreach var in allwilag {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'14_mean+`year'_stab_`var'47_mean)/2
		tab `year'_stab_`var'_aggr
}
}


*NSA Handling 2013
sum b_nsa_handling2 if b_nsa_handling_hd_ges~=.		//static mean, w2
gen b_meannsa_handling2 = r(mean)

sum b_nsa_handling3 if b_nsa_handling_hd_ges~=.		//static mean, w3
gen b_meannsa_handling3 = r(mean)

sum b_nsa_handling4 if b_nsa_handling_hd_ges~=.		//static mean, w4
gen b_meannsa_handling4 = r(mean)

sum b_nsa_handling5 if b_nsa_handling_hd_ges~=.		//static mean, w5
gen b_meannsa_handling5 = r(mean)

sum b_nsa_handling6 if b_nsa_handling_hd_ges~=.		//static mean, w6
gen b_meannsa_handling6 = r(mean)

sum b_nsa_handling7 if b_nsa_handling_hd_ges~=.		//static mean, w7
gen b_meannsa_handling7 = r(mean)




gen b_gesmeannsa_handling = (b_meannsa_handling2 + b_meannsa_handling3 + b_meannsa_handling4 + b_meannsa_handling5 + b_meannsa_handling6 + b_meannsa_handling7)/6
tab b_gesmeannsa_handling [aweight=b_weight]

gen b_gessdnsa_handling = sqrt(((b_meannsa_handling2-b_gesmeannsa_handling)^2 + (b_meannsa_handling3-b_gesmeannsa_handling)^2 + (b_meannsa_handling4-b_gesmeannsa_handling)^2  + (b_meannsa_handling5-b_gesmeannsa_handling)^2  + (b_meannsa_handling6-b_gesmeannsa_handling)^2  + (b_meannsa_handling7-b_gesmeannsa_handling)^2)/6) 
tab b_gessdnsa_handling [aweight=b_weight]


foreach num of numlist 1 4 5 6 7 808{
foreach var in nsa_handling2 nsa_handling3 nsa_handling4 nsa_handling5 nsa_handling6 nsa_handling7 {
	egen b_meanp`var'`num' = mean(b_`var') if b_`var'!=. & b_pidfull1==`num'
}
*
gen b_gesmeannsa_handlingp`num' = (b_meanpnsa_handling2`num' + b_meanpnsa_handling3`num' + b_meanpnsa_handling4`num' + b_meanpnsa_handling5`num' + b_meanpnsa_handling6`num' + b_meanpnsa_handling7`num')/6
tab b_gesmeannsa_handlingp`num' [aweight=b_weight]

gen b_gessdnsa_handlingp`num' = sqrt(((b_meanpnsa_handling2`num'-b_gesmeannsa_handlingp`num')^2 + (b_meanpnsa_handling3`num'-b_gesmeannsa_handlingp`num')^2 + (b_meanpnsa_handling4`num'-b_gesmeannsa_handlingp`num')^2 + (b_meanpnsa_handling5`num'-b_gesmeannsa_handlingp`num')^2 + (b_meanpnsa_handling6`num'-b_gesmeannsa_handlingp`num')^2 + (b_meanpnsa_handling7`num'-b_gesmeannsa_handlingp`num')^2)/6) 
tab b_gessdnsa_handlingp`num' [aweight=b_weight]
}


*Aggregate stability preparation


		foreach num of numlist 2 3 4 5 6 7 {
	gen b_nsa_handling`num'n = round(b_nsa_handling`num'*4)
	tab b_nsa_handling`num'n
}
*

*Differences
gen b_diff_nsa_handling23=b_nsa_handling3n-b_nsa_handling2n
tab b_diff_nsa_handling23

gen b_diff_nsa_handling34=b_nsa_handling4n-b_nsa_handling3n
tab b_diff_nsa_handling34

gen b_diff_nsa_handling45=b_nsa_handling5n-b_nsa_handling4n
tab b_diff_nsa_handling45

gen b_diff_nsa_handling56=b_nsa_handling6n-b_nsa_handling5n
tab b_diff_nsa_handling56

gen b_diff_nsa_handling67=b_nsa_handling7n-b_nsa_handling6n
tab b_diff_nsa_handling67

gen b_stab_nsa_handling23 =1-abs(b_diff_nsa_handling23/4) if b_nsa_handling2n==0 | b_nsa_handling2n==4
replace b_stab_nsa_handling23 =1-abs(b_diff_nsa_handling23/3) if b_nsa_handling2n==1 | b_nsa_handling2n==3
replace b_stab_nsa_handling23 =1-abs(b_diff_nsa_handling23/2) if b_nsa_handling2n==2
tab b_stab_nsa_handling23

gen b_stab_nsa_handling34 =1-abs(b_diff_nsa_handling34/4) if b_nsa_handling3n==0 | b_nsa_handling3n==4
replace b_stab_nsa_handling34 =1-abs(b_diff_nsa_handling34/3) if b_nsa_handling3n==1 | b_nsa_handling3n==3
replace b_stab_nsa_handling34 =1-abs(b_diff_nsa_handling34/2) if b_nsa_handling3n==2
tab b_stab_nsa_handling34

gen b_stab_nsa_handling45 =1-abs(b_diff_nsa_handling45/4) if b_nsa_handling4n==0 | b_nsa_handling4n==4
replace b_stab_nsa_handling45 =1-abs(b_diff_nsa_handling45/3) if b_nsa_handling4n==1 | b_nsa_handling4n==3
replace b_stab_nsa_handling45 =1-abs(b_diff_nsa_handling45/2) if b_nsa_handling4n==2
tab b_stab_nsa_handling45

gen b_stab_nsa_handling56 =1-abs(b_diff_nsa_handling56/4) if b_nsa_handling5n==0 | b_nsa_handling5n==4
replace b_stab_nsa_handling56 =1-abs(b_diff_nsa_handling56/3) if b_nsa_handling5n==1 | b_nsa_handling5n==3
replace b_stab_nsa_handling56 =1-abs(b_diff_nsa_handling56/2) if b_nsa_handling5n==2
tab b_stab_nsa_handling56

gen b_stab_nsa_handling67 =1-abs(b_diff_nsa_handling67/4) if b_nsa_handling6n==0 | b_nsa_handling6n==4
replace b_stab_nsa_handling67 =1-abs(b_diff_nsa_handling67/3) if b_nsa_handling6n==1 | b_nsa_handling6n==3
replace b_stab_nsa_handling67 =1-abs(b_diff_nsa_handling67/2) if b_nsa_handling6n==2
tab b_stab_nsa_handling67

foreach num in 23 34 45 56 67 {
	ci means b_stab_nsa_handling`num'
	gen b_stab_nsa_handling`num'_mean = r(mean)
}

*

*Aggregate stability
foreach year in b {
	foreach var in nsa_handling {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'23_mean+`year'_stab_`var'34_mean+`year'_stab_`var'45_mean+`year'_stab_`var'56_mean+`year'_stab_`var'67_mean)/5
		tab `year'_stab_`var'_aggr
}
}




*Syria 2013
sum b_syria_handling5 if b_syria_handling_hd_ges~=.		//static mean, w5
gen b_meansyria_handling5 = r(mean)

sum b_syria_handling6 if b_syria_handling_hd_ges~=.		//static mean, w6
gen b_meansyria_handling6 = r(mean)

sum b_syria_handling7 if b_syria_handling_hd_ges~=.		//static mean, w7
gen b_meansyria_handling7 = r(mean)

gen b_gesmeansyria_handling = (b_meansyria_handling5 + b_meansyria_handling6 + b_meansyria_handling7)/3
tab b_gesmeansyria_handling [aweight=b_weight]

gen b_gessdsyria_handling = sqrt(((b_meansyria_handling5-b_gesmeansyria_handling)^2 + (b_meansyria_handling6-b_gesmeansyria_handling)^2 + (b_meansyria_handling7-b_gesmeansyria_handling)^2)/3) 
tab b_gessdsyria_handling [aweight=b_weight]


foreach num of numlist 1 4 5 6 7 808{
foreach var in syria_handling5 syria_handling6 syria_handling7 {
	egen b_meanp`var'`num' = mean(b_`var') if b_`var'!=. & b_pidfull1==`num'
}
*
gen b_gesmeansyria_handlingp`num' = (b_meanpsyria_handling5`num' + b_meanpsyria_handling6`num' + b_meanpsyria_handling7`num')/3
tab b_gesmeansyria_handlingp`num' [aweight=b_weight]

gen b_gessdsyria_handlingp`num' = sqrt(((b_meanpsyria_handling5`num'-b_gesmeansyria_handlingp`num')^2 + (b_meanpsyria_handling6`num'-b_gesmeansyria_handlingp`num')^2  + (b_meanpsyria_handling7`num'-b_gesmeansyria_handlingp`num')^2)/3) 
tab b_gessdsyria_handlingp`num' [aweight=b_weight]
}


*Aggregate stability preparation


		foreach num of numlist 5 6 7 {
	gen b_syria_handling`num'n = round(b_syria_handling`num'*4)
	tab b_syria_handling`num'n
}
*

*Differences
gen b_diff_syria_handling56=b_syria_handling6n-b_syria_handling5n
tab b_diff_syria_handling56

gen b_diff_syria_handling67=b_syria_handling7n-b_syria_handling6n
tab b_diff_syria_handling67

gen b_stab_syria_handling56 =1-abs(b_diff_syria_handling56/4) if b_syria_handling5n==0 | b_syria_handling5n==4
replace b_stab_syria_handling56 =1-abs(b_diff_syria_handling56/3) if b_syria_handling5n==1 | b_syria_handling5n==3
replace b_stab_syria_handling56 =1-abs(b_diff_syria_handling56/2) if b_syria_handling5n==2
tab b_stab_syria_handling56

gen b_stab_syria_handling67 =1-abs(b_diff_syria_handling67/4) if b_syria_handling6n==0 | b_syria_handling6n==4
replace b_stab_syria_handling67 =1-abs(b_diff_syria_handling67/3) if b_syria_handling6n==1 | b_syria_handling6n==3
replace b_stab_syria_handling67 =1-abs(b_diff_syria_handling67/2) if b_syria_handling6n==2
tab b_stab_syria_handling67

foreach num in 56 67 {
	ci means b_stab_syria_handling`num'
	gen b_stab_syria_handling`num'_mean = r(mean)
}

*

*Aggregate stability
foreach year in b {
	foreach var in syria_handling {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'56_mean+`year'_stab_`var'67_mean)/2
		tab `year'_stab_`var'_aggr
}
}







*Veggie day
sum b_veggie4 if b_veggie_hd_ges~=.		//static mean, w4
gen b_meanveggie4 = r(mean)

sum b_veggie6 if b_veggie_hd_ges~=.		//static mean, w6
gen b_meanveggie6 = r(mean)

sum b_veggie7 if b_veggie_hd_ges~=.		//static mean, w7
gen b_meanveggie7 = r(mean)


gen b_gesmeanveggie = (b_meanveggie4 + b_meanveggie6 + b_meanveggie7)/3
tab b_gesmeanveggie [aweight=b_weight]

gen b_gessdveggie = sqrt(((b_meanveggie4-b_gesmeanveggie)^2 + (b_meanveggie6-b_gesmeanveggie)^2 + (b_meanveggie7-b_gesmeanveggie)^2)/3) 
tab b_gessdveggie [aweight=b_weight]


foreach num of numlist 1 4 5 6 7 808{
foreach var in veggie4 veggie6 veggie7 {
	egen b_meanp`var'`num' = mean(b_`var') if b_`var'!=. & b_pidfull1==`num'
}
*
gen b_gesmeanveggiep`num' = (b_meanpveggie4`num' + b_meanpveggie6`num' + b_meanpveggie7`num')/3
tab b_gesmeanveggiep`num' [aweight=b_weight]

gen b_gessdveggiep`num' = sqrt(((b_meanpveggie4`num'-b_gesmeanveggiep`num')^2 + (b_meanpveggie6`num'-b_gesmeanveggiep`num')^2  + (b_meanpveggie7`num'-b_gesmeanveggiep`num')^2)/3) 
tab b_gessdveggiep`num' [aweight=b_weight]
}


*Aggregate stability preparation


		foreach num of numlist 4 6 7 {
	gen b_veggie`num'n = round(b_veggie`num'*4)
	tab b_veggie`num'n
}
*

*Differences
gen b_diff_veggie46=b_veggie6n-b_veggie4n
tab b_diff_veggie46

gen b_diff_veggie67=b_veggie7n-b_veggie6n
tab b_diff_veggie67

gen b_stab_veggie46 =1-abs(b_diff_veggie46/4) if b_veggie4n==0 | b_veggie4n==4
replace b_stab_veggie46 =1-abs(b_diff_veggie46/3) if b_veggie4n==1 | b_veggie4n==3
replace b_stab_veggie46 =1-abs(b_diff_veggie46/2) if b_veggie4n==2
tab b_stab_veggie46

gen b_stab_veggie67 =1-abs(b_diff_veggie67/4) if b_veggie6n==0 | b_veggie6n==4
replace b_stab_veggie67 =1-abs(b_diff_veggie67/3) if b_veggie6n==1 | b_veggie6n==3
replace b_stab_veggie67 =1-abs(b_diff_veggie67/2) if b_veggie6n==2
tab b_stab_veggie67

foreach num in 46 67 {
	ci means b_stab_veggie`num'
	gen b_stab_veggie`num'_mean = r(mean)
}

*

*Aggregate stability
foreach year in b {
	foreach var in veggie {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'46_mean+`year'_stab_`var'67_mean)/2
		tab `year'_stab_`var'_aggr
}
}



*Toll 2013
sum b_maut4 if b_maut_hd_ges~=.		//static mean, w4
gen b_meanmaut4 = r(mean)

sum b_maut6 if b_maut_hd_ges~=.		//static mean, w6
gen b_meanmaut6 = r(mean)

sum b_maut7 if b_maut_hd_ges~=.		//static mean, w7
gen b_meanmaut7 = r(mean)

gen b_gesmeanmaut = (b_meanmaut4 + b_meanmaut6 + b_meanmaut7)/3
tab b_gesmeanmaut [aweight=b_weight]

gen b_gessdmaut = sqrt(((b_meanmaut4-b_gesmeanmaut)^2 + (b_meanmaut6-b_gesmeanmaut)^2 + (b_meanmaut7-b_gesmeanmaut)^2)/3) 
tab b_gessdmaut [aweight=b_weight]


foreach num of numlist 1 4 5 6 7 808{
foreach var in maut4 maut6 maut7 {
	egen b_meanp`var'`num' = mean(b_`var') if b_`var'!=. & b_pidfull1==`num'
}
*
gen b_gesmeanmautp`num' = (b_meanpmaut4`num' + b_meanpmaut6`num' + b_meanpmaut7`num')/3
tab b_gesmeanmautp`num' [aweight=b_weight]

gen b_gessdmautp`num' = sqrt(((b_meanpmaut4`num'-b_gesmeanmautp`num')^2 + (b_meanpmaut6`num'-b_gesmeanmautp`num')^2  + (b_meanpmaut7`num'-b_gesmeanmautp`num')^2)/3) 
tab b_gessdmautp`num' [aweight=b_weight]
}


*Aggregate stability preparation


		foreach num of numlist 4 6 7 {
	gen b_maut`num'n = round(b_maut`num'*4)
	tab b_maut`num'n
}
*

*Differences
gen b_diff_maut46=b_maut6n-b_maut4n
tab b_diff_maut46

gen b_diff_maut67=b_maut7n-b_maut6n
tab b_diff_maut67

gen b_stab_maut46 =1-abs(b_diff_maut46/4) if b_maut4n==0 | b_maut4n==4
replace b_stab_maut46 =1-abs(b_diff_maut46/3) if b_maut4n==1 | b_maut4n==3
replace b_stab_maut46 =1-abs(b_diff_maut46/2) if b_maut4n==2
tab b_stab_maut46

gen b_stab_maut67 =1-abs(b_diff_maut67/4) if b_maut6n==0 | b_maut6n==4
replace b_stab_maut67 =1-abs(b_diff_maut67/3) if b_maut6n==1 | b_maut6n==3
replace b_stab_maut67 =1-abs(b_diff_maut67/2) if b_maut6n==2
tab b_stab_maut67

foreach num in 46 67 {
	ci means b_stab_maut`num'
	gen b_stab_maut`num'_mean = r(mean)
}

*

*Aggregate stability
foreach year in b {
	foreach var in maut {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'46_mean+`year'_stab_`var'67_mean)/2
		tab `year'_stab_`var'_aggr
}
}







*Credibility refusal red-red-green coalition
sum b_glaubw_rrg3 if b_glaubw_rrg_hd_ges~=.		//static mean, w3
gen b_meanglaubw_rrg3 = r(mean)

sum b_glaubw_rrg5 if b_glaubw_rrg_hd_ges~=.		//static mean, w5
gen b_meanglaubw_rrg5 = r(mean)

sum b_glaubw_rrg6 if b_glaubw_rrg_hd_ges~=.		//static mean, w6
gen b_meanglaubw_rrg6 = r(mean)


gen b_gesmeanglaubw_rrg = (b_meanglaubw_rrg3 + b_meanglaubw_rrg5 + b_meanglaubw_rrg6)/3
tab b_gesmeanglaubw_rrg [aweight=b_weight]

gen b_gessdglaubw_rrg = sqrt(((b_meanglaubw_rrg3-b_gesmeanglaubw_rrg)^2 + (b_meanglaubw_rrg5-b_gesmeanglaubw_rrg)^2 + (b_meanglaubw_rrg6-b_gesmeanglaubw_rrg)^2)/3) 
tab b_gessdglaubw_rrg [aweight=b_weight]


foreach num of numlist 1 4 5 6 7 808{
foreach var in glaubw_rrg3 glaubw_rrg5 glaubw_rrg6 {
	egen b_meanp`var'`num' = mean(b_`var') if b_`var'!=. & b_pidfull1==`num'
}
*
gen b_gesmeanglaubw_rrgp`num' = (b_meanpglaubw_rrg3`num' + b_meanpglaubw_rrg5`num' + b_meanpglaubw_rrg6`num')/3
tab b_gesmeanglaubw_rrgp`num' [aweight=b_weight]

gen b_gessdglaubw_rrgp`num' = sqrt(((b_meanpglaubw_rrg3`num'-b_gesmeanglaubw_rrgp`num')^2 + (b_meanpglaubw_rrg5`num'-b_gesmeanglaubw_rrgp`num')^2  + (b_meanpglaubw_rrg6`num'-b_gesmeanglaubw_rrgp`num')^2)/3) 
tab b_gessdglaubw_rrgp`num' [aweight=b_weight]
}


*Aggregate stability preparation


		foreach num of numlist 3 5 6 {
	gen b_glaubw_rrg`num'n = round(b_glaubw_rrg`num'*4)
	tab b_glaubw_rrg`num'n
}
*

*Differences
gen b_diff_glaubw_rrg35=b_glaubw_rrg5n-b_glaubw_rrg3n
tab b_diff_glaubw_rrg35

gen b_diff_glaubw_rrg56=b_glaubw_rrg6n-b_glaubw_rrg5n
tab b_diff_glaubw_rrg56

gen b_stab_glaubw_rrg35 =1-abs(b_diff_glaubw_rrg35/4) if b_glaubw_rrg3n==0 | b_glaubw_rrg3n==4
replace b_stab_glaubw_rrg35 =1-abs(b_diff_glaubw_rrg35/3) if b_glaubw_rrg3n==1 | b_glaubw_rrg3n==3
replace b_stab_glaubw_rrg35 =1-abs(b_diff_glaubw_rrg35/2) if b_glaubw_rrg3n==2
tab b_stab_glaubw_rrg35

gen b_stab_glaubw_rrg56 =1-abs(b_diff_glaubw_rrg56/4) if b_glaubw_rrg5n==0 | b_glaubw_rrg5n==4
replace b_stab_glaubw_rrg56 =1-abs(b_diff_glaubw_rrg56/3) if b_glaubw_rrg5n==1 | b_glaubw_rrg5n==3
replace b_stab_glaubw_rrg56 =1-abs(b_diff_glaubw_rrg56/2) if b_glaubw_rrg5n==2
tab b_stab_glaubw_rrg56

foreach num in 35 56 {
	ci means b_stab_glaubw_rrg`num'
	gen b_stab_glaubw_rrg`num'_mean = r(mean)
}

*

*Aggregate stability
foreach year in b {
	foreach var in glaubw_rrg {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'35_mean+`year'_stab_`var'56_mean)/2
		tab `year'_stab_`var'_aggr
}
}


*Percentage intra-individual change

foreach year in a b {
	foreach var in allwilag {
		tab `year'_`var'_hd_ges `year'_rohkoeffkat_`var' [aw=`year'_weight], cell
}
}

foreach year in b {
	foreach var in nsa_handling syria_handling veggie maut glaubw_rrg {
		tab `year'_`var'_hd_ges `year'_rohkoeffkat_`var' [aw=`year'_weight], cell
}
}

*Drop unnecessary aiding variables

foreach year in a b{
	foreach var in allwilag {
	drop `year'_diff_`var'??
	drop `year'_stab_`var'??
	drop `year'_stab_`var'??_mean
	drop `year'_`var'?n
}
}


foreach year in b{
	foreach var in nsa_handling syria_handling veggie maut glaubw_rrg {
	drop `year'_diff_`var'??
	drop `year'_stab_`var'??
	drop `year'_stab_`var'??_mean
	drop `year'_`var'?n
}
}




save "${data}\data2009_2013 participants 7 waves.dta", replace

	

